class Solution {
    public boolean isPalindrome(String s) {
        if (s.length() <= 1) return true;
        return doubleFor(s);
    }
    private boolean doubleFor(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (Character.isLetter(s.charAt(i)) || Character.isDigit(s.charAt(i))) {
                if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') {
                    sb.append((char)(s.charAt(i) - 'A' + 'a'));
                } else {
                    sb.append(s.charAt(i));
                }
            }
        }
        s = sb.toString();
        for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }
    private boolean singleFor(String s) {
        for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
            char a = s.charAt(i);
            char b = s.charAt(j);
            if (!Character.isLetter(a) && !Character.isDigit(a)) {
                j++;
                continue;
            }
            if (!Character.isLetter(b) && !Character.isDigit(b)) {
                i--;
                continue;
            }
            if (a <= 'Z' && a >= 'A')
                a = (char)(a - 'A' + 'a');
            if (b <= 'Z' && b >= 'A')
                b = (char)(b - 'A' + 'a');
            if (a != b) {
                return false;
            }
        }
        return true;
    }
}
